/*
 * @ProjectName: 编程学习
 * @Copyright:   2019 HangZhou Yi Dev, Ltd. All Right Reserved.
 * @address:     https://yiyuery.github.io/
 * @date:        2019/5/20 20:57
 * @email:       xiazhaoyang@live.com
 * @description: 本内容仅限于编程技术学习使用，转发请注明出处.
 */
package com.example.template.pattern.singleton;

/**
 * <p>
 *  双重检查加锁实现单例模式
 * </p>
 *
 * @author Yi
 * @version v1.0.0
 * @date 2019-08-18 21:46
 * @modificationHistory=========================逻辑或功能性重大变更记录
 * @modify By: {修改人} 2019-08-18
 * @modify reason: {方法名}:{原因}
 * ...
 */
public class DoubleLockSingleton {

    private volatile static DoubleLockSingleton instance;

    private DoubleLockSingleton() {
    }

    public static synchronized DoubleLockSingleton getInstance() {
        //检查单例是否已存在
        if (instance == null) {
            //多线程进入获取初始化实例时
            synchronized (DoubleLockSingleton.class) {
                //进入区块后，再检查一次，如果还为null，才创建实例
                if (instance == null) {
                    instance = new DoubleLockSingleton();
                }
            }
        }
        return instance;
    }
}
